package top.guxx.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanUtil;
import top.guxx.springframework.beans.BeansException;
import top.guxx.springframework.beans.PropertyValue;
import top.guxx.springframework.beans.PropertyValues;
import top.guxx.springframework.beans.factory.config.BeanDefinition;
import top.guxx.springframework.beans.factory.config.BeanReference;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author：Guxx
 * @Description：
 * @Date：2024/6/8 21:24
 * @Filename：AbstractAutoWireCapableBeanFactoy
 */
public abstract class AbstractAutoWireCapableBeanFactory extends  AbstractBeanFactory{
    private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();

    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException {
        Object bean = null;
        try{
            bean = createBeanInstance(beanName, beanDefinition, args);
            applyPropertyValues(beanName, bean, beanDefinition);
            //作业1 如果需要添加有参的函数对象，如何解决？ 大概思路
           /* Class<?> clazz = beanDefinition.getBeanClass();
            Constructor<?> constructor = beanClass.getDeclaredConstructor(beanClass, int.class);
            Object o = constructor.newInstance(1);*/
        }catch (Exception e){
            throw new BeansException("Instantiation of beans failed",e);
        }
        addSingleton(beanName,bean);
        return bean;
    }
    protected Object createBeanInstance(String beanName, BeanDefinition beanDefinition, Object... args) throws BeansException {
        Constructor constructorToUse = null;
        Class<?> beanClass = beanDefinition.getBeanClass();
        Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
        for (Constructor<?> ctor : declaredConstructors) {
            if (null != args && ctor.getParameterTypes().length == args.length){
                constructorToUse = ctor;
                break;
            }
        }
        //return instantiationStrategy.instantiate(beanDefinition, beanName, constructorToUse, args);
        return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args);
    }

    //Bean属性填充
    protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition){
        try {
            PropertyValues propertyValues = beanDefinition.getPropertyValues();
            for (PropertyValue propertyValue : propertyValues.getPropertyValuesArray()){
                String name = propertyValue.getName();
                Object value = propertyValue.getValue();

                if (value instanceof BeanReference){
                    // A 依赖 B，获取B的实例化
                    BeanReference beanReference = (BeanReference) value;
                    value = getBean(beanReference.getBeanName());
                }
                BeanUtil.setFieldValue(bean, name, value);
            }
        } catch (Exception e) {
            throw new BeansException("Error setting property values：", e);
        }
    }

    public InstantiationStrategy getInstantiationStrategy() {
        return instantiationStrategy;
    }

    public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
        this.instantiationStrategy = instantiationStrategy;
    }
}
